<?php

class Api_Form_Validator_EmailExist extends Zend_Validate_Abstract
{
    const NOT_MATCH = 'emailExist';

    /**
     * Entity manager of Doctrine ORM
     * 
     * @var Doctrine\ORM\EntityManager
     */
    private $_em;

    protected $_messageTemplates = array(
        self::NOT_MATCH => 'Email already exists'
    );

    public function __construct()
    {
        $this->_em = Zend_Registry::get('doctrine')->getEntityManager();
    }

    public function isValid($value)
    {
        $value = (string) $value;
        $user = $this->_em->getRepository('Api\Core\Entity\SmshubUsers')
                ->findOneByEmailAddress($value);
        $valid = false;

        if (isset($user)) {
            $valid = true;
            $this->_error(self::NOT_MATCH);
        }
        return !$valid;
    }
}